﻿using System.Collections.Generic;
using System.Linq;
using ML.Entity;
using ML.IRepository;
using Dapper;

namespace ML.Repository
{
    public class PlatformCategoryRepository : DBFactory, IPlatformCategoryRepository
    {
        public Pager<PlatformCategoryEntity> List(PlatFormCategorySearch pfcSearch)
        {
            //组装调用存储过程参数
            var criteria = new PageCriteria() { TableName = "ml_platformcategory" };
            if (!string.IsNullOrEmpty(pfcSearch.Name))
            {
                criteria.Conditions += string.Format(" AND Name LIKE'%{0}%'", pfcSearch.Name);
            }
            criteria.Conditions += string.Format(" AND DelMark=0");

            criteria.PageIndex = pfcSearch.pageindex;
            criteria.Field = @"Id,Name,PreviewImg,Sort,Enable,DelMark,CreateTime";
            criteria.PageSize = pfcSearch.pagesize;
            criteria.SortName = pfcSearch.ordername;
            if (pfcSearch.orderby)
            {
                criteria.Sort = "DESC";
            }
            else
            {
                criteria.Sort = "ASC";
            }

            //调用存储过程进行查询并且返回
            var list = GetPageData<PlatformCategoryEntity>(criteria);
            return list;
        }

        public IEnumerable<PlatformCategoryEntity> GetAllEnableList()
        {
            const string sql = @"SELECT * FROM ml_platformcategory WHERE Enable = 0 AND DelMark = 0 ORDER BY Sort";
            using (_db)
            {
                return _db.Query<PlatformCategoryEntity>(sql);
            }
        }

        public PlatformCategoryEntity GetById(string id)
        {
            const string sql = @"SELECT * FROM ml_platformcategory Where Id=@Id";
            using (_db)
            {
                return _db.Query<PlatformCategoryEntity>(sql, new { @Id = id }).FirstOrDefault();
            }
        }

        public bool Add(PlatformCategoryEntity entity)
        {
            const string sql = @"INSERT INTO ml_platformcategory(Id, Name, PreviewImg, Sort, Enable, DelMark, CreateTime)
                VALUES(@Id, @Name, @PreviewImg, @Sort, @Enable, @DelMark, @CreateTime)";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Update(PlatformCategoryEntity entity)
        {
            const string sql = @"UPDATE ml_platformcategory Set Name=@Name,PreviewImg=@PreviewImg,Sort=@Sort,Enable=@Enable WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Delete(PlatformCategoryEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(string id)
        {
            const string sql = @"UPDATE ml_platformcategory Set DelMark=@DelMark WHERE Id=@Id";
            using (_db)
            {
                if (_db.Execute(sql, new { @DelMark = (int)DeleteStatus.delete_true, @Id = id }) > 0)
                {
                    return true;
                }
                return false;
            }
        }
    }
}